home *** CD-ROM | disk | FTP | other *** search
/ Into His Marvelous Light / Into His Marvelous LIGHT.iso / lesson1.dxr / 00081_Script_Fade In-Out < prev    next >
Text File  |  2001-09-05  |  10KB  |  269 lines

  1. -- Fade In/Out
  2. -- Fades a sprite between two fade values once, multiple times, or indefinitely.
  3. -- v1 - 9 October 1998 by Darrel Plant
  4.  
  5. on getBehaviorDescription
  6.   vDesc = "FADE IN/OUT" & RETURN & RETURN
  7.   vDesc = vDesc & "Gives the appearance of a sprite fading in or out."
  8.   vDesc = vDesc && "Can be used with text, bitmap, animated GIF, vector"
  9.   vDesc = vDesc && "shapes, and Shockwave Flash sprites."
  10.   vDesc = vDesc && "Choose whether the sprite should first appear"
  11.   vDesc = vDesc && "at maximum (faded in) or minimum (faded out) values,"
  12.   vDesc = vDesc && "when the fading should start, the minimum and maximum"
  13.   vDesc = vDesc && "fade values, the number of times it should"
  14.   vDesc = vDesc && "fade, and how fast it should fade."
  15.   vDesc = vDesc && "The fade can be activated automatically in the first frame,"
  16.   vDesc = vDesc && "by a click on the sprite, or by sending the sprite a"
  17.   vDesc = vDesc && "mFadeActivate message. Set the number of cycles to -1"
  18.   vDesc = vDesc && "if you want an endless loop, or 0 if the fade should"
  19.   vDesc = vDesc && "happen only once." & RETURN&RETURN
  20.   vDesc = vDesc & "PARAMETERS:" & RETURN
  21.   vDesc = vDesc & " - Is sprite faded in or out at beginning" & RETURN
  22.   vDesc = vDesc & " - Fade activation (automatic, click, message)" & RETURN
  23.   vDesc = vDesc & " - Maximum fade value" & RETURN
  24.   vDesc = vDesc & " - Minimum fade value" & RETURN
  25.   vDesc = vDesc & " - Fade cycles" & RETURN
  26.   vDesc = vDesc & " - Fade speed (seconds)" & RETURN&RETURN
  27.   vDesc = vDesc & "PERMITTED TYPES:" & RETURN
  28.   vDesc = vDesc & "#text, #bitmap, #animgif, #vectorShape, #flash" & RETURN
  29.   return vDesc
  30. end getBehaviorDescription
  31.  
  32. on getBehaviorTooltip
  33.   vTip = "Fades a sprite between two fade values" &RETURN
  34.   vTip = vTip & "once, multiple times, or indefinitely."&RETURN&RETURN
  35.   vTip = vTip &"The fade can be initiated automatically,"&RETURN
  36.   vTip=vTip & "by mouse click, or via a message to the sprite."
  37.   return vTip
  38. end getBehaviorTooltip
  39.  
  40. -- PROPERTIES --
  41.  
  42. property pSprite                  -- sprite object reference
  43. property pStart                   -- time last fade started
  44. property pActive                  -- activity flag for fade action
  45. property pCompleteCycles          -- counter for fade cycles
  46. property pFadeDiff                -- difference between maximum and minimum fade values
  47. -- author-defined parameters
  48. property pFaded                   -- does sprite begin fade in or out
  49. property pAuto                    -- when does fade begin
  50. property pFadeMax                 -- maximum fade value
  51. property pFadeMin                 -- minimum fade value
  52. property pCycles                  -- number of times to repeat fade
  53. property pPeriodBase              -- author-defined fade period
  54. property pPeriod                  -- fade period in milliseconds
  55.  
  56. -- EVENT HANDLERS --
  57.  
  58. on beginSprite me
  59.   mInitialize me
  60. end beginSprite
  61.  
  62. on prepareFrame me
  63.   mUpdate me
  64. end prepareFrame
  65.  
  66. on mouseUp me
  67.   if pAuto = #click then mActivate me
  68. end mouseUp
  69.  
  70. -- CUSTOM HANDLERS --
  71.  
  72. on mInitialize me
  73.   -- determine sprite reference
  74.   pSprite = sprite (me.spriteNum)
  75.   -- flag for fade action
  76.   pActive = #off
  77.   -- initialize cycle counter
  78.   pCompleteCycles = 0.5
  79.   -- convert user-set period to milliseconds
  80.   pPeriod = pPeriodBase * 1000
  81.   -- ensure that maximum is greater than minimum
  82.   if pFadeMax < pFadeMin then
  83.     -- if max is less than min, swap values
  84.     vMax = pFadeMax
  85.     pFadeMax = pFadeMin
  86.     pFadeMin = vMax
  87.   end if
  88.   -- determine difference between max and min
  89.   pFadeDiff = pFadeMax - pFadeMin
  90.   -- if first fade action is 'in', sprite needs to be faded 'out' first
  91.   if pFaded = #in then
  92.     pSprite.blend = pFadeMin
  93.   else
  94.     pSprite.blend = pFadeMax
  95.   end if
  96.   -- activate sprite if automatic activation is set
  97.   if pAuto = #automatic then mActivate me
  98. end mInitialize
  99.  
  100. on mUpdate me
  101.   -- only update sprite if active flag is set
  102.   if pActive <> #off then
  103.     -- derive current value of millisecond timer
  104.     vMillis = the milliseconds
  105.     -- determine milliseconds elapsed since fade start
  106.     vElapsed = vMillis - pStart
  107.     -- compare elapsed time to fade period
  108.     if vElapsed >= pPeriod then
  109.       -- time has elapsed, fade should finish
  110.       -- set sprite to value for end of fade action
  111.       case pActive of
  112.         #in: pSprite.blend = pFadeMax
  113.         #out: pSprite.blend = pFadeMin
  114.       end case
  115.       -- deactivate activity flag
  116.       pActive = #off
  117.       -- increment cycle counter and reverse if necessary
  118.       mCheckCycle me
  119.     else
  120.       -- scale difference between fade values to elapsed time
  121.       vFadeDiff = integer (pFadeDiff * vElapsed / pPeriod)
  122.       -- depending on current value of activity flag, set blend
  123.       -- to starting point of flag action plus or minus scaled
  124.       -- difference between fade values
  125.       case pActive of
  126.         #in: pSprite.blend = pFadeMin + vFadeDiff
  127.         #out: pSprite.blend = pFadeMax - vFadeDiff
  128.       end case
  129.     end if
  130.   end if
  131. end mUpdate
  132.  
  133. on mActivate me
  134.   -- if pFadeDiff (the difference between pFadeMax and pFadeMin) is 0
  135.   -- then fade is never activated
  136.   if pFadeDiff then
  137.     -- start fade actions
  138.     -- test whether sprite should fade in or out as first action
  139.     case pFaded of
  140.         -- intiate fade in action
  141.       #in: mFadeIn me
  142.         -- initiate fade out action
  143.       #out: mFadeOut me
  144.     end case
  145.   end if
  146. end mActivate
  147.  
  148. on mCheckCycle me
  149.   -- used to determine whether to initiate another fade action
  150.   -- this is checked after each fade action
  151.   -- set continuation flag
  152.   vContinue = FALSE
  153.   -- check cycle setting
  154.   case pCycles of
  155.       -- if value is -1, sprite will cycle forever, set flag to TRUE
  156.     -1: vContinue = TRUE
  157.       -- if value is 0, sprite should stop after first fade action
  158.     0: vContinue = FALSE
  159.     otherwise
  160.       -- any other value will cycle 1 to 10 times
  161.       -- each fade action increments counter by 0.3
  162.       pCompleteCycles = pCompleteCycles + 0.3
  163.       -- compare counter to total number of cycles
  164.       if integer (pCompleteCycles) <= pCycles then
  165.         -- if counter is less than numberof cycles, continue fading
  166.         vContinue = TRUE
  167.       end if
  168.   end case
  169.   -- if continuation flag has been set, then determine which way to fade
  170.   if vContinue then
  171.     -- compare current blend value to maximum fade
  172.     if pSprite.blend = pFadeMax then
  173.       -- if they are equal, fade sprite out
  174.       mFadeOut me
  175.     else
  176.       -- otherwise, fade sprite in
  177.       mFadeIn me
  178.     end if
  179.   end if
  180. end mCheckCycle
  181.  
  182. on mFadeIn me
  183.   mFade me, #in
  184. end mFadeIn
  185.  
  186. on mFadeOut me, vTarget
  187.   mFade me, #out
  188. end mFadeOut
  189.  
  190. on mFade me, vInOut
  191.   -- general-purpose fade handler
  192.   -- set activity flag
  193.   pActive = vInOut
  194.   -- start fade timer
  195.   pStart = the milliseconds
  196. end mFade
  197.  
  198. on mErrorAlert me, vError, vData
  199.   -- based on James Newton's error checking procedure
  200.   -- determine name of behavior
  201.   vBehaviorname = string (me)
  202.   delete word 1 of vBehaviorName
  203.   delete the last word of vBehaviorName
  204.   delete the last word of vBehaviorName
  205.   -- convert supporting data
  206.   case vData.ilk of
  207.     #void: vData = "<void>"
  208.     #symbol: vData = "#" & vData
  209.   end case
  210.   case vError of
  211.       -- deal with individual error types
  212.     #getPDLError:  -- error from property dialog
  213.       beep
  214.       return [#getPDLError: [#comment:  ¼
  215.         "CANCEL: Sprite MUST be one of the following member types:" ¼
  216.          & RETURN & vData, #format: #symbol,  ¼
  217.          #range: [#Cancel], #default: #Cancel]]
  218.     #runTimeError: -- called if wrong sprite type is detected in beginSprite
  219.       alert "CANCEL: Sprite MUST be one of the following member types:" ¼
  220.          & RETURN & mPermittedMemberTypes ()
  221.   end case
  222. end mErrorAlert
  223.  
  224. --PUBLIC METHOD
  225. --use this method to send a message to the sprite and initiate 
  226. --the Fade In or Out. 
  227. --Example:
  228. --sendSprite(1, #mFadeActivate)
  229.  
  230. on mFadeActivate me
  231.   -- message sent to activate sprite if not auto or by click
  232.   if pAuto = #message then mActivate me
  233. end mFadeActivate
  234.  
  235. -- AUTHOR-DEFINED PARAMETERS
  236.  
  237. on getPropertyDescriptionList me
  238.   if not the currentSpriteNum then
  239.     -- behavior has been attached to script channel
  240.     exit
  241.   end if
  242.   vMember = sprite (the currentSpriteNum).member
  243.   vMemberType = vMember.type
  244.   vPermitted = mPermittedMemberTypes ()
  245.   if not vPermitted.getPos (vMemberType) then
  246.     -- sprite member is not of the correct type
  247.     return mErrorAlert (me, #getPDLError, vPermitted)
  248.   end if
  249.   vPDList = [:]
  250.   setaProp vPDList, #pFaded, [#comment: "Fade in or out?", #format: #symbol, ¼
  251.     #default: #in, #range: [#in, #out]]
  252.   setaProp vPDList, #pFadeMax, [#comment: "Maximum Fade Value", #format: #integer, ¼
  253.     #default: 100, #range: [#min: 0, #max: 100]]
  254.   setaProp vPDList, #pFadeMin, [#comment: "Minimum Fade Value", #format: #integer, ¼
  255.     #default: 0, #range: [#min: 0, #max: 100]]
  256.   setaProp vPDList, #pAuto, [#comment: "Start automatically, when clicked," && ¼
  257.     "or by message?", #default: #automatic, #format: #symbol, ¼
  258.     #range: [#automatic, #click, #message]]
  259.   setaProp vPDList, #pCycles, [#comment: "Fade cycles (0 = one" && ¼
  260.     "fade only, -1 = repeat forever)", #format: #integer,  ¼
  261.     #default: 0, #range: [#min: -1, #max:10]]
  262.   setaProp vPDList, #pPeriodBase, [#comment: "Time period for fade (seconds)", ¼
  263.     #format: #float, #default: 2.0, #range: [#min: .25, #max: 15.0]]
  264.   return vPDList
  265. end getPropertyDescriptionList
  266.  
  267. on mPermittedMemberTypes me
  268.   return [#text, #bitmap, #animgif, #vectorShape, #flash]
  269. end mPermittedMemberTypes